﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class MotionaCamera : MonoBehaviour
{
    public static MotionaCamera instance;
    public float m_speed;

    private Vector3 _rotation;
    private Transform _podTrans;

    private bool wrong_bool = true;//是否补差
    private bool mouseDown_bool;//鼠标按下时是否在指定的范围内
    public float rotation_Speed;//视角旋转灵敏度

    internal bool 第一视角_bool;
    internal bool 第三视角_bool = true;//默认第三视角

    public Vector3 cameraStartPot_V3;//相机初始位置
    public Vector3 cameraStartRot_V3;//相机初始角度
    void Start()
    {
        instance = this;
        _podTrans = transform;
    }

    void Update()
    {
        

    }
    private void FixedUpdate()
    {
        if (第一视角_bool)
        {
            if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.UpArrow))
            {
                _podTrans.Translate(Vector3.forward * m_speed * Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.S) | Input.GetKey(KeyCode.DownArrow))
            {
                _podTrans.Translate(Vector3.back * m_speed * Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.LeftArrow))
            {
                _podTrans.Translate(Vector3.left * m_speed * Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.D) | Input.GetKey(KeyCode.RightArrow))
            {
                _podTrans.Translate(Vector3.right * m_speed * Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.E))
            {
                _podTrans.Translate(Vector3.up * 15 * Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.Q))
            {
                _podTrans.Translate(Vector3.down * 15 * Time.deltaTime);
            }

            //Debug.Log(Input.GetAxis("Mouse X"));
            if ((Input.mousePosition.x > 380f && Input.mousePosition.y > 215f&& Input.mousePosition.x < 1505f && Input.mousePosition.y < 990f) || mouseDown_bool)
            {

                if (Input.GetMouseButton(1))
                {
                    if (!mouseDown_bool)
                    {
                        mouseDown_bool = true;
                    }
                    //var mouseX = Input.GetAxis("Mouse X");
                    //var mouseY = Input.GetAxis("Mouse Y");
                    if (wrong_bool)
                    {
                        _podTrans.DOKill(false);
                        wrong_bool = false;
                        //Debug.Log(_podTrans.localEulerAngles.x);

                        //鼠标每次按下时进行矫正
                        _rotation.x = CheckAngle(_podTrans.localEulerAngles.x);
                        _rotation.y = CheckAngle(_podTrans.localEulerAngles.y);
                        //_rotation = _podTrans.eulerAngles.x >= 229.90?new Vector3(-30,_podTrans.eulerAngles.y,0) : _podTrans.eulerAngles;//每次鼠标按下矫正旋转
                    }
                    _rotation.y += 360 * Time.deltaTime * Input.GetAxis("Mouse X") * rotation_Speed;

                    _rotation.x = Mathf.Clamp(_rotation.x - 360 * Time.deltaTime * Input.GetAxis("Mouse Y") * rotation_Speed, -30, 50);


                    _podTrans.localEulerAngles = _rotation;
                }
                if (Input.GetAxis("Mouse ScrollWheel") < 0)
                {
                    if (Camera.main.fieldOfView <= 60)
                        Camera.main.fieldOfView += 3;
                }
                //Zoom in
                if (Input.GetAxis("Mouse ScrollWheel") > 0)
                {
                    if (Camera.main.fieldOfView > 40)
                        Camera.main.fieldOfView -= 3;
                }
            }

            if (Input.GetMouseButtonUp(1))
            {
                mouseDown_bool = false;
                wrong_bool = true;
            }
        }

        if (第三视角_bool)
        {

        }
      
    }

    public float CheckAngle(float value)
    {
        float angle = value - 180;

        if (angle > 0)
            return angle - 180;

        return angle + 180;
    }

    //碰撞开始
    private void OnCollisionEnter(Collision collision)
    {
        //Debug.Log(collision.transform.name);
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.name);
    }
}
